package com.study.spark.scala.chapter07

import scala.collection.mutable
import scala.collection.mutable.ListBuffer

object Scala06_Collection_Seq_1{
  def main(args: Array[String]): Unit = {
    // TODO 集合 - ListBuffer
    // 可变的List集合:ListBuffer
    // TODO 操作数据的基本方法
    val lb = ListBuffer(1,2,3,4,5)
    val lb1 = ListBuffer(3,2,1)
    println(lb)//ListBuffer(1, 2, 3, 4, 5)
    lb.append(6)
    println(lb)//ListBuffer(1, 2, 3, 4, 5, 6)
    lb.appendAll(lb1)
    println(lb)//ListBuffer(1, 2, 3, 4, 5, 6, 3, 2, 1)
    lb.insert(1,20)
    println(lb)//ListBuffer(1, 20, 2, 3, 4, 5, 6, 3, 2, 1)
    lb.update(2,30)
    println(lb)
    //ListBuffer(1, 20, 30, 3, 4, 5, 6, 3, 2, 1)
    val lb2: ListBuffer[Int] = lb.updated(3,30)
    println(lb2)//ListBuffer(1, 20, 30, 30, 4, 5, 6, 3, 2, 1)
    lb2.remove(0)
    println(lb2)//ListBuffer(20, 30, 30, 4, 5, 6, 3, 2, 1)
    lb2.remove(1,3)
    println(lb2)//ListBuffer(20, 5, 6, 3, 2, 1)

    // 遍历
    println(lb2.mkString(","))//20,5,6,3,2,1
    lb2.foreach(println)
    /*
    20
    5
    6
    3
    2
    1
     */

    // List和ListBuffer的转换
    val list =List(1,2,3)
    println(list)//List(1, 2, 3)
    val listBuffer: mutable.Buffer[Int] = list.toBuffer
    println(listBuffer)//ArrayBuffer(1, 2, 3)
    val list1: List[Int] = listBuffer.toList
    println(list1)//ArrayBuffer(1, 2, 3)
  }
}
